<?php

include "../../../lib/start.php";

check_session();
check_permission(DOC_PERM);

if(!isset($_REQUEST['l2'])){
	// anni in archivio
	$sel_anni = "SELECT anno FROM stud_works GROUP BY anno ORDER BY anno DESC";
	$res_anni = $db->execute($sel_anni);
	
	$sel_classi = "SELECT id_classe, classe, sezione FROM classi ORDER BY sezione, classe";
	$res_classi = $db->execute($sel_classi);
}
else{
	if(!isset($_REQUEST['offset']))
	    $offset = 0;
	else
	    $offset = $_REQUEST['offset'];
	
	$limit = 10;
	
	$query = "SELECT id_work, anno, data_inserimento, autore, file, _classe AS classe, abstract, titolo, CONCAT(utenti.nome, utenti.cognome) AS docente FROM stud_works, utenti WHERE utenti.uid = stud_works.owner ";
	if($_REQUEST['anno'] != "all" && $_REQUEST['anno'] != "")
		$query .= "AND anno = '".$_REQUEST['anno']."' ";
	if($_REQUEST['classe'] != "all" && $_REQUEST['classe'] != "")
		$query .= "AND _classe = ".$_REQUEST['classe']." ";
	if(trim($_REQUEST['studente']) != "")
		$query .= "AND autore LIKE '%".strtoupper($_REQUEST['studente'])."%' ";
	if(trim($_REQUEST['docente']) != "")
		$query .= "HAVING docente LIKE '%".strtoupper($_REQUEST['docente'])."%' ";
	switch($_REQUEST['order']){
		case "1":
			$query .= " ORDER BY anno DESC, classe , autore";
			break;
		case "2":
			$query .= " ORDER BY _classe , anno DESC, autore";
			break;
		case "3":
			$query .= " ORDER BY autore, anno DESC";
			break;
		case "4":
			$query .= " ORDER BY docente, anno DESC, _classe ";
			break;
		default:
			$query .= " ORDER BY anno DESC";
			break;
	}
	//$files = mysql_query($query);
	if(!isset($_REQUEST['second'])){
	    $files = $db->execute($query);
	    //print $sel_links;
	    $count = $files->num_rows;
	    //print $count;
	    $_SESSION['count_files'] = $count;
	}
	else{
	    $query .= " LIMIT $limit OFFSET $offset";
	    $files = $db->execute($query);
	}
	//print $query;
	if($offset == 0)
	    $page = 1;
	else
	    $page = ($offset / $limit) + 1;
	
	$pagine = ceil($_SESSION['count_files'] / $limit);
	if($pagine < 1)
	    $pagine = 1;
}

// dati per la paginazione (navigate.php)
$colspan = 4;
$link = basename($_SERVER['PHP_SELF']);
$count_name = "count_files";

include "lavori_studenti.html.php";

?>